﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;

namespace YhmOA.Common
{
    /// <summary>
    /// 页面常用数据源
    /// </summary>
    public class DataSourceConfig
    {
        /// <summary>
        /// 根据类型获取数据源
        /// </summary>
        /// <param name="key">类型名称</param>
        /// <returns>以逗号分割的数据源</returns>
        public static string GetDataSource(string key)
        {
            return GetDictionary()[key];
        }

        /// <summary>
        /// 获取xml的路径
        /// </summary>
        private static string XmlPath
        {
            get
            {
                string file = HttpContext.Current.Server.MapPath("~") + "Config\\DataSource.xml";
                return file;
            }
        }

        private static Dictionary<string, string> GetDictionary()
        {
            if (DataCache.GetCache("____DataSourceConfig") == null)
                DataCache.SetCache("____DataSourceConfig", AnalysisXML());
            return DataCache.GetCache("____DataSourceConfig") as Dictionary<string, string>;
        }

        #region 解析xml
        /// <summary>
        /// 解析xml
        /// </summary>
        private static Dictionary<string, string> AnalysisXML()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(XmlPath);
            XmlNodeList xmlList = xmlDoc.SelectNodes("DataSources/DataSource");
            foreach (XmlNode item in xmlList)
            {
                dic.Add((item as XmlElement).GetAttribute("name"), (item as XmlElement).GetAttribute("value"));
            }
            return dic;
        }
        #endregion
    }
}
